﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

using FreeFlow.Administration;

namespace FreeFlowAdministrator
{
  class WorkflowInstancesAdaptor : Adaptor, IRefreshable
  {
    private WorkflowInstanceCollection workflowInstances;
    public WorkflowInstancesAdaptor(WorkflowInstanceCollection workflowInstances)
    {
      this.workflowInstances = workflowInstances;
    }

    public override string Text
    {
      get 
      {
        return "Workflow instances (" + workflowInstances.Count + ")";
      }
    }

    public override object Object
    {
      get 
      {
        return workflowInstances; 
      }
    }

    public override int ImageIndex
    {
      get
      {
        return 9;
      }
    }

    public override void AddChildren(TreeNode node)
    {
      foreach (WorkflowInstance wf in workflowInstances)
      {
        // versions
        TreeNode wfNode = TreeNodeFactory.CreateTreeNode(new WorkflowInstanceAdaptor(wf));
        node.Nodes.Add(wfNode);
      }
    }

    public override Control GetRenderer()
    {
      WorkflowInstancesControl control = new WorkflowInstancesControl();
      control.LoadInstances(workflowInstances);
      control.RefreshRequired += new EventHandler(TriggerRefreshRequired);
      return control;
    }

    #region IRefreshable Members

    public void Refresh()
    {
      workflowInstances.Refresh();
    }

    #endregion
  }
}
